package com.tfleo.mealssenger.utility;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.tfleo.mealssenger.Main;
import com.tfleo.mealssenger.object.Comment;
import com.tfleo.mealssenger.object.Food;
import com.tfleo.mealssenger.object.Guest;
import com.tfleo.mealssenger.object.Hangout;
import com.tfleo.mealssenger.object.News;
import com.tfleo.mealssenger.object.Notice;
import com.tfleo.mealssenger.object.Place;
import com.tfleo.mealssenger.object.FriendSelection;
import com.tfleo.mealssenger.object.PlaceSimple;
import com.tfleo.mealssenger.object.Suggestion;
import com.tfleo.mealssenger.object.User;

public class DataFetcher {
	/**
	 * Fetch all places info from server and instantiate each as Place object.
	 * 
	 * @return array of Place objects.
	 */
	public static ArrayList<Place> fetchNearby(double longitude, double latitude, int sort, 
			boolean isFiltered, String filter) {
		ArrayList<Place> places = new ArrayList<Place>();
		StringBuilder sb = new StringBuilder(Main.SERVER_URL + "place/nearby&lonlat=" + longitude + "," + latitude);
		try {
			URL url;
			if(sort == 1) {
				sb.append("&sortbyrating=1");
				//url = new URL(Main.SERVER_URL + "place/nearby&lonlat=" + longitude + "," + latitude + "&sortbyrating=1");
			}
			
			if(isFiltered) {
				sb.append("&filter=" + filter);
			}
			
			url = new URL(sb.toString());
			URLConnection conn = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");
	
					for (int i = 0; i < ja.length(); i++) {
						JSONObject jplace = (JSONObject) ja.get(i);
						Place place = new Place(jplace.getInt("id"),
								jplace.getString("name"),
								jplace.getString("address"), 
								jplace.getString("description"),
								jplace.getString("image"),
								jplace.getString("category"), 
								jplace.getInt("time"), 
								Float.parseFloat(jplace.getString("longitude")), 
								Float.parseFloat(jplace.getString("latitude")),
								jplace.getInt("num_checkin"),
								Float.parseFloat(jplace.getString("rating")));
						
						place.setDistance(jplace.getInt("distance"));
						places.add(place);
					}
					return places;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
		
		return null;
	}
	
	/**
	 * Fetch place details given its id info from server and instantiate each as Place object.
	 * 
	 * @return Place object.
	 */
	public static Place fetchPlace(int id)
	{
		try {
			URL url = new URL(Main.SERVER_URL + "place/" + id);
			URLConnection conn = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			String line;
			if((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				
				if(jo.getInt("code") == 1) {
					JSONObject jplace = jo.getJSONObject("response");
					Place place = new Place(jplace.getInt("id"),
							jplace.getString("name"),
							jplace.getString("address"), 
							jplace.getString("description"),
							jplace.getString("image"),
							jplace.getString("category"), 
							jplace.getInt("time"), 
							Float.parseFloat(jplace.getString("longitude")), 
							Float.parseFloat(jplace.getString("latitude")),
							jplace.getInt("num_checkin"),
							Float.parseFloat(jplace.getString("rating")));
					
					return place;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return null;
	}
	
	/**
	 * Fetch recommended foods info from server and instantiate each as Food object.
	 * 
	 * @return array of Place objects.
	 */
	public static ArrayList<Food> fetchRecommendation(int userId, double longitude, double latitude, String auth,
			boolean isFiltered, String category) {
		ArrayList<Food> foods = new ArrayList<Food>();

		try {
			StringBuilder sb = new StringBuilder(Main.SERVER_URL + "food/recommendation&lonlat=" + longitude + "," +
					latitude + "&user_id=" + userId + "&auth=" + auth);
			
			if(isFiltered) {
				sb.append("&category=" + category);
			}
			URL url = new URL(sb.toString());
			URLConnection tc = url.openConnection();
			
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");
	
					for (int i = 0; i < ja.length(); i++) {
						JSONObject jfood = (JSONObject) ja.get(i);
	
						Food food = new Food(jfood.getInt("id"),
								jfood.getString("name"),
								jfood.getInt("location_id"),
								jfood.getString("location_name"),
								jfood.getString("description"),
								jfood.getString("image"),
								jfood.getString("category"),
								jfood.getString("tag"),
								jfood.getInt("num_mealup"),
								Float.parseFloat(jfood.getString("rating")));
						
						foods.add(food);
					}
					
					if(foods.size() == 0) {
						Food food = new Food(0,
								"No food found!",
								0,
								"",
								"",
								"0.jpg");
						foods.add(food);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return foods;
	}
	
	/**
	 * Fetch food details from server given its id.
	 * 
	 * @return Food object.
	 */
	public static Food fetchFood(int id)
	{
		try {
			URL url = new URL(Main.SERVER_URL + "food/" + id);
			URLConnection conn = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			String body;
			if((body = in.readLine()) != null) {
				JSONObject jo = new JSONObject(body);
				
				if(jo.getInt("code") == 1) {
					JSONObject response = jo.getJSONObject("response");
					
					Food food = new Food(response.getInt("id"),
							response.getString("name"),
							response.getInt("location_id"),
							response.getString("location_name"),
							response.getString("description"),
							response.getString("image"),
							response.getString("category"),
							response.getString("tag"),
							response.getInt("num_mealup"),
							Float.parseFloat(response.getString("rating")));
					
					return food;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return null;
	}
	
	public static ArrayList<Food> fetchMenu(int placeId) {
		ArrayList<Food> foods = new ArrayList<Food>();

		try {
			URL url = new URL(Main.SERVER_URL + "place/" + placeId + "/food");
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String body;
			while ((body = in.readLine()) != null) {
				JSONObject jo = new JSONObject(body);
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");

					for (int i = 0; i < ja.length(); i++) {
						JSONObject foodBody = (JSONObject) ja.get(i);

						Food food = new Food(foodBody.getInt("id"),
								foodBody.getString("name"),
								foodBody.getInt("location_id"),
								foodBody.getString("location_name"),
								foodBody.getString("description"),
								foodBody.getString("image"),
								foodBody.getString("category"),
								foodBody.getString("tag"),
								foodBody.getInt("num_mealup"),
								Float.parseFloat(foodBody.getString("rating")));
						
						foods.add(food);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return foods;
	}
	
	public static ArrayList<Food> fetchWantedFoods(int userId, int me, String auth) {
		ArrayList<Food> foods = new ArrayList<Food>();

		try {
			URL url = new URL(Main.SERVER_URL + "user/" + userId + "/wanteds&me=" + me + "&auth=" + auth);
			URLConnection tc = url.openConnection();
			//tc.setUseCaches(true); //this one cause reply to be cached if using 3G
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");
				
					for (int i = 0; i < ja.length(); i++) {
						JSONObject jfood = (JSONObject) ja.get(i);
	
						Food food = new Food(jfood.getInt("id"),
								jfood.getString("name"),
								jfood.getInt("location_id"),
								jfood.getString("location_name"),
								jfood.getString("description"),
								jfood.getString("image"),
								jfood.getString("category"),
								jfood.getString("tag"),
								jfood.getInt("num_mealup"),
								Float.parseFloat(jfood.getString("rating")));
						
						foods.add(food);
					}
					return foods;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	public static ArrayList<Comment> fetchFoodComment(int foodId) {
		ArrayList<Comment> comments = new ArrayList<Comment>();

		try {
			URL url = new URL(Main.SERVER_URL + "food/" + foodId + "/comments");
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true); //3G cache problem
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String body;
			while ((body = in.readLine()) != null) {
				JSONObject jo = new JSONObject(body);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");
				
					for (int i = 0; i < ja.length(); i++) {
						JSONObject comm = (JSONObject) ja.get(i);
	
						Comment comment = new Comment(comm.getInt("id"),
								comm.getInt("user_id"),
								comm.getString("user_name"),
								comm.getInt("food_id"),
								comm.getString("comment"),
								Float.parseFloat(comm.getString("rating")),
								comm.getString("date"));
						
						comments.add(comment);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return comments;
	}
	
	public static int fetchMealup(int foodId) {
		int like;

		try {
			URL url = new URL(Main.SERVER_URL + "food/" + foodId + "/mealup");
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject response = new JSONObject(line);
				
				if((like = response.getInt("likes")) > 0) {
					return like;
				} else {
					return -1;
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return -1;
	}
	
	public static ArrayList<Comment> fetchPlaceComment(int placeId) {
		ArrayList<Comment> comments = new ArrayList<Comment>();

		try {
			URL url = new URL(Main.SERVER_URL + "place/" + placeId + "/comments");
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String body;
			while ((body = in.readLine()) != null) {
				JSONObject jo = new JSONObject(body);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");
				
					for (int i = 0; i < ja.length(); i++) {
						JSONObject comm = (JSONObject) ja.get(i);
	
						Comment comment = new Comment(comm.getInt("id"),
								comm.getInt("user_id"),
								comm.getString("user_name"),
								comm.getInt("place_id"),
								comm.getString("comment"),
								Float.parseFloat(comm.getString("rating")),
								comm.getString("date"));
						
						comments.add(comment);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return comments;
	}
	
	public static ArrayList<News> fetchNews(int me, String auth) {
		ArrayList<News> newsList = new ArrayList<News>();

		try {
			URL url = new URL(Main.SERVER_URL + "user/socialnews&me=" + me + "&auth=" + auth);
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");

					for (int i = 0; i < ja.length(); i++) {
						JSONObject newsObj = (JSONObject) ja.get(i);
	
						News news = new News(newsObj.getInt("user_id"),
								newsObj.getString("user_name"),
								newsObj.getString("user_image"),
								newsObj.getInt("story_id"),
								newsObj.getString("story_name"),
								newsObj.getInt("story_type"),
								newsObj.getString("date"));
						
						newsList.add(news);
					}
					return newsList;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	public static ArrayList<News> fetchOwnNews(int userId, int me, String auth) {
		ArrayList<News> newsList = new ArrayList<News>();

		try {
			URL url = new URL(Main.SERVER_URL + "user/" + userId + "/news&me=" + me + "&auth=" + auth);
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");

					for (int i = 0; i < ja.length(); i++) {
						JSONObject newsObj = (JSONObject) ja.get(i);
	
						News news = new News(newsObj.getInt("user_id"),
								newsObj.getString("user_name"),
								newsObj.getString("user_image"),
								newsObj.getInt("story_id"),
								newsObj.getString("story_name"),
								newsObj.getInt("story_type"),
								newsObj.getString("date"));
						
						newsList.add(news);
					}
					return newsList;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return newsList;
	}
	
	public static ArrayList<Notice> fetchNotice(int userId, String auth) {
		ArrayList<Notice> noteList = new ArrayList<Notice>();
		//noteList.add(new Notice(0, "", 0, "")); //default notice is 'ask suggestion'

		try {
			URL url = new URL(Main.SERVER_URL + "user/notice&user_id=" + userId + "&auth=" + auth);
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");

					for (int i = 0; i < ja.length(); i++) {
						JSONObject jnote = (JSONObject) ja.get(i);
	
						Notice note = new Notice(jnote.getInt("notice_type"),
								jnote.getString("subject_name"),
								jnote.getInt("notice_id"),
								jnote.getString("notice_name"));
						
						noteList.add(note);
					}
					noteList.add(0, new Notice(0, "", noteList.size(), "" + noteList.size()));
					return noteList;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	public static Suggestion fetchSuggestion(int me, double longitude, double latitude, String auth) {
		ArrayList<FriendSelection> fs = new ArrayList<FriendSelection>();
		ArrayList<Place> ps = new ArrayList<Place>();
		Suggestion s = new Suggestion(ps, fs);
		
		try {
			URL url = new URL(Main.SERVER_URL + "hangout/suggestion&me=" + me + 
					"&lon=" + longitude + "&lat=" + latitude + "&auth=" + auth);
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				if(jo.getInt("code") == 1) {
					JSONObject jresp = jo.getJSONObject("response");
					
					JSONArray ja1 = jresp.getJSONArray("place");
					
					for (int i = 0; i < ja1.length(); i++) {
						JSONObject jplace= (JSONObject) ja1.get(i);

						Place p = new Place( jplace.getInt("id"),
								jplace.getString("name"),
								jplace.getString("address"), 
								jplace.getString("description"),
								jplace.getString("image"),
								jplace.getString("category"), 
								jplace.getInt("time"), 
								Float.parseFloat(jplace.getString("longitude")), 
								Float.parseFloat(jplace.getString("latitude")),
								jplace.getInt("num_checkin"),
								Float.parseFloat(jplace.getString("rating")));
						p.setDistance(jplace.getInt("distance"));
						p.setMessage(jplace.optString("food"));
						ps.add(p);
						
					
					}
					
					JSONArray ja2 = jresp.getJSONArray("friend");
					
					for (int i = 0; i < ja2.length(); i++) {
						JSONObject jfriend = (JSONObject) ja2.get(i);

						FriendSelection f = new FriendSelection(
								jfriend.getInt("id"),
								jfriend.getString("name"),
								jfriend.getString("image"));
						f.setLocation(Float.parseFloat(jfriend.getString("lat")), 
								Float.parseFloat(jfriend.getString("lon")));
						fs.add(f);
					
					}
				}
				
			}
			s = new Suggestion(ps, fs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return s;
	}
	
	public static Suggestion fetchFreeSuggestion(int me, double longitude, double latitude, String auth) {
		ArrayList<FriendSelection> fs = new ArrayList<FriendSelection>();
		ArrayList<Place> ps = new ArrayList<Place>();
		Suggestion s = new Suggestion(ps, fs);
		
		try {
			URL url = new URL(Main.SERVER_URL + "hangout/suggestionfree&me=" + me +
					"&lon=" + longitude + "&lat=" + latitude + "&auth=" + auth);
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				if(jo.getInt("code") == 1) {
					JSONObject jresp = jo.getJSONObject("response");
					
					JSONArray ja1 = jresp.getJSONArray("place");
					
					for (int i = 0; i < ja1.length(); i++) {
						JSONObject jplace= (JSONObject) ja1.get(i);

						Place p = new Place( jplace.getInt("id"),
								jplace.getString("name"),
								jplace.getString("address"), 
								jplace.getString("description"),
								jplace.getString("image"),
								jplace.getString("category"), 
								jplace.getInt("time"), 
								Float.parseFloat(jplace.getString("longitude")), 
								Float.parseFloat(jplace.getString("latitude")),
								jplace.getInt("num_checkin"),
								Float.parseFloat(jplace.getString("rating")));
						p.setDistance(jplace.getInt("distance"));
						p.setMessage(jplace.optString("food"));
						ps.add(p);
					
					}
					
					JSONArray ja2 = jresp.getJSONArray("friend");
					
					for (int i = 0; i < ja2.length(); i++) {
						JSONObject jfriend = (JSONObject) ja2.get(i);

						FriendSelection f = new FriendSelection(
								jfriend.getInt("id"),
								jfriend.getString("name"),
								jfriend.getString("image"));
						f.setLocation(Float.parseFloat(jfriend.getString("lat")), 
								Float.parseFloat(jfriend.getString("lon")));
						fs.add(f);
					
					}
				}
				
			}
			s = new Suggestion(ps, fs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return s;
	}
	
	/**
	 * Fetch friend user details from server and instantiate each as User object.
	 * 
	 * @return User object.
	 */
	public static User fetchUser(int id, int me, String auth) {
		try {
			URL url = new URL(Main.SERVER_URL + "user/" + id + "&me=" + me + "&auth=" + auth);
			URLConnection conn = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			String body;
			if((body = in.readLine()) != null) {
				JSONObject jo = new JSONObject(body);
				if(jo.getInt("code") == 1) {
					JSONObject response = jo.getJSONObject("response");
					User user = new User(response.getInt("id"),
							response.getString("name"),
							response.getString("image"), 
							response.optInt("last_checkin"),
							response.optInt("place_id"),
							response.optString("place_name"));
					
					return user;
				} else {
					return null;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return null;
	}
	
	public static ArrayList<User> fetchFriends(int userId, int me, String auth) {
		ArrayList<User> friends = new ArrayList<User>();

		try {
			URL url = new URL(Main.SERVER_URL + "/user/" + userId + "/friends&me=" + me + "&auth=" + auth);
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String body;
			while ((body = in.readLine()) != null) {
				JSONObject jo = new JSONObject(body);
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");
	
					for (int i = 0; i < ja.length(); i++) {
						JSONObject obj = (JSONObject) ja.get(i);
	
						User friend = new User(obj.getInt("id"),
								obj.getString("name"),
								obj.getString("image"),
								0,
								0,
								"");
						
						friends.add(friend);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return friends;
	}
	
	public static ArrayList<Hangout> fetchHangoutList(int userId, int me, String auth) {
		ArrayList<Hangout> hangoutList = new ArrayList<Hangout>();

		try {
			URL url = new URL(Main.SERVER_URL + "user/" + userId + "/hangout&me=" + me + "&auth=" + auth);
			URLConnection tc = url.openConnection();
			//tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");

					for (int i = 0; i < ja.length(); i++) {
						JSONObject jhang = (JSONObject) ja.get(i);
	
						Hangout hangout = new Hangout(jhang.getInt("id"),
								jhang.getString("place_name"),
								jhang.getInt("num_guests"),
								jhang.getString("date"));
						
						hangoutList.add(hangout);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return hangoutList;
	}
	
	public static Hangout fetchHangout(int id) {
		try {
			URL url = new URL(Main.SERVER_URL + "hangout/" + id);
			URLConnection conn = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			String line;
			if((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				if(jo.getInt("code") == 1) {
					JSONObject jhang = jo.getJSONObject("response");
					
					Hangout hangout = new Hangout(jhang.getInt("id"),
							jhang.getInt("host_id"),
							jhang.getString("host_name"),
							jhang.getString("host_image"),
							jhang.getInt("place_id"),
							jhang.getString("place_name"),
							jhang.getString("place_address"),
							jhang.getString("place_image"),
							jhang.getString("place_lon"),
							jhang.getString("place_lat"),
							jhang.getInt("num_guests"),
							jhang.getString("message"),
							jhang.getString("date"));
					
					return hangout;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return null;
	}
	
	public static ArrayList<Guest> fetchGuestList(int id) {
		ArrayList<Guest> guestList = new ArrayList<Guest>();

		try {
			URL url = new URL(Main.SERVER_URL + "hangout/" + id + "/guests");
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONObject jo = new JSONObject(line);
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");
	
					for (int i = 0; i < ja.length(); i++) {
						JSONObject jguest = (JSONObject) ja.get(i);
	
						Guest guest = new Guest(jguest.getInt("guest_id"),
								jguest.getString("guest_name"),
								jguest.getString("guest_image"),
								jguest.getBoolean("is_attending"));
						
						guestList.add(guest);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return guestList;
	}
	
	public static ArrayList<FriendSelection> fetchFriendSelection(int userId, int me, String auth) {
		ArrayList<FriendSelection> friends = new ArrayList<FriendSelection>();

		try {
			URL url = new URL(Main.SERVER_URL + "/user/" + userId + "/friends&me=" + me + "&auth=" + auth);

			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String body;
			while ((body = in.readLine()) != null) {
				JSONObject jo = new JSONObject(body);
				if(jo.getInt("code") == 1) {
					JSONArray ja = jo.getJSONArray("response");
	
					for (int i = 0; i < ja.length(); i++) {
						JSONObject obj = (JSONObject) ja.get(i);
	
						FriendSelection friend = new FriendSelection(obj.getInt("id"),
								obj.getString("name"),
								obj.getString("image"));
						friend.setLocation(Float.parseFloat(obj.getString("lat")), 
								Float.parseFloat(obj.getString("lon")));
						friends.add(friend);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return friends;
	}
	
	public static HashMap<Integer, String> fetchPlaceGroup() {
		HashMap<Integer, String> groupMap = new HashMap<Integer, String>();

		try {
			URL url = new URL(Main.SERVER_URL + "/place_group.php");
			URLConnection tc = url.openConnection();
			tc.setUseCaches(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

			String line;
			while((line = in.readLine()) != null) {
				JSONArray ja = new JSONArray(line);

				for(int i = 0; i < ja.length(); i++) {
					JSONObject jo = (JSONObject) ja.get(i);

					groupMap.put(jo.getInt("id"), jo.getString("name"));
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return groupMap;
	}
}
